package com.example.easeimpro.controller.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.easeimpro.R;
import com.example.easeimpro.controller.fragment.MyEaseChatFragment;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.modules.chat.EaseChatMessageListLayout;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.easeui.widget.chatrow.EaseChatRowText;

import java.util.List;

public class ChatSendMsgActivity extends BaseActivity {

    private String conversationId;
    private int conversationType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_send_msg);

        initData();
      initLisenter();
    }

    private void initLisenter() {
        EaseTitleBar titleBar = findViewById(R.id.friend_titlebar);
        titleBar.setTitle(conversationId);
        titleBar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                finish();
            }
        });
    }

    private static final String TAG = "ChatSendMsgActivity";
    private void initData() {
        conversationId = getIntent().getStringExtra("conversationId");
        conversationType = getIntent().getIntExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_SINGLE);
        Log.e(TAG, "initData: "+conversationType );
        //创建会话fragment
        MyEaseChatFragment fragment = new MyEaseChatFragment();

        Bundle bundle = new Bundle();
        if(conversationType == EaseConstant.CHATTYPE_GROUP){

            List<EMGroup> group = EMClient.getInstance().groupManager().getAllGroups();
            for(EMGroup emg : group){
                if(emg.getGroupName().equals(conversationId)){
                    conversationId = emg.getGroupId();
                }
            }

            bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID,conversationId);
            bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
        } else if (conversationType == EaseConstant.CHATTYPE_CHATROOM) {
            bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
            bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_CHATROOM);
        }else {
            bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
            bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
        }
        fragment.setArguments(bundle);

       //showToast(fragment.chatLayout+"");
        FragmentTransaction chat = getSupportFragmentManager().beginTransaction().replace(R.id.sendMsg_fl_fragment, fragment, "chat");
        chat.commit();



//        fragment.setArguments(getIntent().getExtras());
//
//        getSupportFragmentManager().beginTransaction().add(0, fragment).commit();
    }
}